#include "switch_drv_adapter.h"
#define LOG_TAG "SWITCH_DRV_ADAPTER"
#include "elog.h"

switch_options_t g_switch_fan_options_adapter_api;
switch_options_t g_switch_light_options_adapter_api;


TaskHandle_t            switch_fan_task_handler;
TaskHandle_t            switch_light_task_handler;

void switch_drv_adapter_light_process_task_entry(void* p);
void switch_drv_adapter_fan_process_task_entry(void* p);

static volatile bool switch_drv_status = true;

/**
 * @desc  : 开关适配层初始化
 * @date  : 2025-11-11
 * @author: AnRui
 * @note  :
 */
void switch_drv_adapter_init(void)
{
    log_d("switch drv adapter init start");
    if (!switch_drv_init()) {
        log_e("switch drv init error");
			  switch_drv_status = false;
    } 

    g_switch_fan_options_adapter_api.p_switch_open = switch_fan_open;
    g_switch_fan_options_adapter_api.p_switch_close = switch_fan_close;
    g_switch_light_options_adapter_api.p_switch_open = switch_light_open;
    g_switch_light_options_adapter_api.p_switch_close = switch_light_close;

    // 灯光控制任务
    if (xTaskCreate(switch_drv_adapter_light_process_task_entry,
        "switch_light_adapter_task",
        ADA_SWITCH_LIGHT_TASK_STACK_SIZE,
        NULL,
        ADA_SWITCH_LIGHT_TASK_PRIORITY,
        &switch_light_task_handler) != pdTRUE) {
        log_e("create switch light adapter task failed");
    }

    // 风扇控制任务
    if (xTaskCreate(switch_drv_adapter_fan_process_task_entry,
        "switch_fan_adapter_task",
        ADA_SWITCH_FAN_TASK_STACK_SIZE,
        NULL,
        ADA_SWITCH_FAN_TASK_PRIORITY,
        &switch_fan_task_handler) != pdTRUE) {
        log_e("create switch fan adapter task failed");
    }
    log_d("switch drv adapter init end");
}

void switch_drv_adapter_light_process_task_entry(void* p)
{
    midlayer_switch_light_t* p_mid_data = (midlayer_switch_light_t*)midlayer_switch_light_mmc_ctrl.p_shared_mem;
    bool res;
    while (1) {
        //等待来自中间层的控制请求
        xSemaphoreTake(midlayer_switch_light_mmc_ctrl.sem_req, portMAX_DELAY);
			  log_d("resv midlayer cmd,light_status : %s",
                p_mid_data->light_status == LIGHT_STATUS_ON ? "电灯打开" : "电灯关闭");
        // 如果设备初始化失败
        if (!switch_drv_status) {
            p_mid_data->light_status = LIGHT_STATUS_FAULT;
        } else {
            if (p_mid_data->light_status == LIGHT_STATUS_ON)  res = g_switch_light_options_adapter_api.p_switch_open();
            if (p_mid_data->light_status == LIGHT_STATUS_OFF)  res = g_switch_light_options_adapter_api.p_switch_close();
					  if (!res){
							  p_mid_data->light_status = LIGHT_STATUS_FAULT;
								log_e("switch light fault");
						}
        }
        // 回复来自中间层的控制请求
        xSemaphoreGive(midlayer_switch_light_mmc_ctrl.sem_ack);
    }

}

void switch_drv_adapter_fan_process_task_entry(void* p)
{
    midlayer_switch_fan_t* p_mid_data = (midlayer_switch_fan_t*)midlayer_switch_fan_mmc_ctrl.p_shared_mem;
    bool res;
    while (1) {
        //等待来自中间层的控制请求
        xSemaphoreTake(midlayer_switch_fan_mmc_ctrl.sem_req, portMAX_DELAY);
				log_d("resv midlayer cmd,fan_status : %s",
                p_mid_data->fan_status == FAN_STATUS_ON ? "风扇打开" : "风扇关闭");
        // 如果设备初始化失败
        if (!switch_drv_status) {
            p_mid_data->fan_status = FAN_STATUS_FAULT;
					  log_e("switch fan drv init error");
        } else {
            if (p_mid_data->fan_status == FAN_STATUS_ON)  res = g_switch_fan_options_adapter_api.p_switch_open();
            if (p_mid_data->fan_status == FAN_STATUS_OFF)  res = g_switch_fan_options_adapter_api.p_switch_close();
            if (!res){
							  p_mid_data->fan_status = FAN_STATUS_FAULT;
								log_e("switch fan fault");
						}
        }
        // 回复来自中间层的控制请求
        xSemaphoreGive(midlayer_switch_fan_mmc_ctrl.sem_ack);
    }

}
